﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using METTest;
using System.Reflection;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using HelloAttribute;
using System.ComponentModel;

namespace MEFConsoleApplication
{
    [Export]
    class Program
    {
        [ImportMany]
        //public IEnumerable<IHelloWord> HelloWord { get; set; }
        public Lazy<IHelloWord,IHelloWordCapabilities>[] HelloRowrd { get; set; }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Method();
        }

        public void Method()
        {
            AggregateCatalog catelog = new AggregateCatalog();
            catelog.Catalogs.Add(new DirectoryCatalog(Directory.GetCurrentDirectory()));//查找部件，当前应用程序

            catelog.Catalogs.Add(new DirectoryCatalog(@"../../../METTest1/bin/Debug"));//这个我们通过路径找到部件
            catelog.Catalogs.Add(new DirectoryCatalog(@"../../../METTest/bin/Debug"));//这个我们通过路径找到部件
            //catelog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            CompositionContainer container = new CompositionContainer(catelog);//声明容器
            container.ComposeParts(this);//把容器中的部件组合到一起
            //CompositionBatch Batch = new CompositionBatch();
            //Batch.AddPart(this);
            //container.Compose(Batch);
            //HelloWord = container.GetExportedValue<IHelloWord>();//这样也可以实例化借口
            //Console.WriteLine(HelloWord.SayHello("eric"));
            //Console.WriteLine(HelloWord.SayWord("_eric"));
            //foreach (var item in HelloWord)
            //{
            //    Console.WriteLine(item.SayHello("eric"));
            //}

            Console.WriteLine(HelloRowrd.FirstOrDefault().Value.SayHello("baby"));
            Console.WriteLine(HelloRowrd.FirstOrDefault().Metadata.Transport);

            #region list 排序
            List<Person> lp = new List<Person>();
            Random random = new Random();
            Person p = new Person();
            for(int i = 20; i >= 0; i--)
            {
                p = new Person()
                {
                    FirstName = GetRangeChineseCode(3),
                    LastName = "",
                    Birthday = DateTime.Now.AddYears(0 - random.Next(10, 95))
                };
                lp.Add(p);
                Console.WriteLine(p.FirstName + p.LastName + "\t" + p.Birthday.ToString("yyyy-MM-dd"));
            }
            lp.Sort((x, y) => { return x.Age - y.Age; });
            Console.WriteLine("--------------------------------------------------------------------");
            lp.ForEach(item =>
            {
                Console.WriteLine(item.FirstName + item.LastName + "\t" + item.Birthday.ToString("yyyy-MM-dd"));
            });
            #endregion

            Console.Read();
        }

        #region 随机获取指定长度的文字
        /// <summary>
        /// 随机获邓指定长度的文字
        /// </summary>
        /// <param name="strlength"></param>
        /// <returns></returns>
        public string GetRangeChineseCode(int strlength)
        {
            //定义一个字符串数组储存汉字编码的组成元素 
            string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

            Random rnd = new Random();

            //定义一个object数组用来 
            object[] bytes = new object[strlength];

            /**/
            /*每循环一次产生一个含两个元素的十六进制字节数组，并将其放入bject数组中 
         每个汉字有四个区位码组成 
         区位码第1位和区位码第2位作为字节数组第一个元素 
         区位码第3位和区位码第4位作为字节数组第二个元素 
        */
            for (int i = 0; i < strlength; i++)
            {
                //区位码第1位 
                int r1 = rnd.Next(11, 14);
                string str_r1 = rBase[r1].Trim();

                //区位码第2位 
                rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值 
                int r2;
                if (r1 == 13)
                {
                    r2 = rnd.Next(0, 7);
                }
                else
                {
                    r2 = rnd.Next(0, 16);
                }
                string str_r2 = rBase[r2].Trim();

                //区位码第3位 
                rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
                int r3 = rnd.Next(10, 16);
                string str_r3 = rBase[r3].Trim();

                //区位码第4位 
                rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
                int r4;
                if (r3 == 10)
                {
                    r4 = rnd.Next(1, 16);
                }
                else if (r3 == 15)
                {
                    r4 = rnd.Next(0, 15);
                }
                else
                {
                    r4 = rnd.Next(0, 16);
                }
                string str_r4 = rBase[r4].Trim();

                //定义两个字节变量存储产生的随机汉字区位码 
                byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
                byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
                //将两个字节变量存储在字节数组中 
                byte[] str_r = new byte[] { byte1, byte2 };

                //将产生的一个汉字的字节数组放入object数组中 
                bytes.SetValue(str_r, i);

            }
            Encoding gb = Encoding.GetEncoding("gb2312");
            string strOutput = string.Empty;

            //根据汉字编码的字节数组解码出中文汉字 
            for (int i = 0; i < strlength; i++)
            {
                strOutput += gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
            }
            return strOutput;
        }

        /// 生成随机码
        /// </summary>
        /// <param name="length">随机码个数</param>
        /// <returns></returns>
        public string GetRandomCode(int length)
        {
            int rand;
            char code;
            string randomcode = String.Empty;

            //生成一定长度的验证码
            System.Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                rand = random.Next();

                if (rand % 3 == 0)
                {
                    code = (char)('A' + (char)(rand % 26));
                }
                else
                {
                    code = (char)('0' + (char)(rand % 10));
                }

                randomcode += code.ToString();
            }
            return randomcode;
        }

        #endregion
    }
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age
        {
            get
            {
                return DateTime.Now.Year - Birthday.Year;
            }
        }
        public DateTime Birthday { get; set; }
    }
    public interface IHelloWordCapabilities
    {
        [DefaultValue(HelloTransport.PhoneNetwork)]
        HelloTransport Transport { get; }
        [DefaultValue(false)]
        bool IsSecure { get; }
    }
}